﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using HotelSoft.HotelManagement_DTO;
using HotelSoft.HotelManagement_BUS;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmHoadonThanhToan : Form
    {
        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        // Override the CreateParams property
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        Double temp = 1;
        HoaDoanThanhToanBUS busDT = new HoaDoanThanhToanBUS();
        ArrayList dsHD;
        DataTable kqBaoCao = new DataTable();
        BindingSource bsData = null;
        private static string SoPDK;
        public string SOPHIEUDK
        {
            get { return SoPDK; }
            set { SoPDK = value; }
        }

        public frmHoadonThanhToan()
        {
            InitializeComponent();
        }

        private void frmTraCuu_Hoadon_Load(object sender, EventArgs e)
        {
            CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone(); 
            culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"; 
            Thread.CurrentThread.CurrentCulture = culture;
            timer1.Enabled = true;

            ////MessageBox.Show(SoPDK);
            //kqBaoCao = busDT.HoaDonThanhToan();
            LayDanhSachHD();
            //gvDS.DataSource = kqBaoCao.DefaultView;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
                timer1.Enabled = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInDS_Click(object sender, EventArgs e)
        {
            if (txtSoHDin.Text.Trim() == "" || txtSoHDin.Text.Length == 0)
            {
                MessageBox.Show("Xin hãy nhập số hoá đơn muốn in", "Thông báo: in hoá đơn");
                txtSoHDin.Focus();
                return;
            }
            frmReport_HoaDon frm = new frmReport_HoaDon();
            frm.SoHD = txtSoHDin.Text;
            frm.ShowDialog();
        }

        private void XuatDanhSachHD(ArrayList dsHD)
        {
            gvDS.AutoGenerateColumns = false;

            this.bsData = new BindingSource(dsHD, "");
            gvDS.DataSource = bsData;
        }

        private void LayDanhSachHD()
        {
            busDT = new HoaDoanThanhToanBUS();
            dsHD = busDT.getHoaDonThanhToan();
            kqBaoCao = ChuyenDoi.ToDataTable(dsHD);
            XuatDanhSachHD(dsHD);
        }

        private void Btn_TraCuu_Click(object sender, EventArgs e)
        {
            HoaDonThanhToanDTO dtoHD = new HoaDonThanhToanDTO();

            if (txtMaNV.Text == "")
                dtoHD.m_MaNV = null;
            else
                dtoHD.m_MaNV = txtMaNV.Text;

            if (txtHoTenKH.Text == "")
                dtoHD.m_TenKH = null;
            else
                dtoHD.m_TenKH = txtHoTenKH.Text;

            if (txtHoTenNV.Text == "")
                dtoHD.m_TenNVThu = null;
            else
                dtoHD.m_TenNVThu = txtHoTenNV.Text;

            if (txtCMND.Text == "")
                dtoHD.m_MaKH = null;
            else
                dtoHD.m_MaKH = txtCMND.Text;

            if (txtTongTien.Text == "")
                dtoHD.m_TongTien = 0;
            else
                dtoHD.m_TongTien = float.Parse(txtTongTien.Text);

            if (dtNgayLapHD.Value == null)
                dtoHD.m_NgayThanhToan = null;
            else
                dtoHD.m_NgayThanhToan = dtNgayLapHD.Text;

            if (txtSoHD.Text == "")
                dtoHD.m_MaHD = null;
            else
                dtoHD.m_MaHD = txtSoHD.Text;

            if (txtSoPDK.Text == "")
                dtoHD.m_MaPDK = null;
            else
                dtoHD.m_MaPDK = txtSoPDK.Text;


            ArrayList dsTraCuuDuoc = busDT.TraCuuHD(dsHD, dtoHD);
            kqBaoCao = ChuyenDoi.ToDataTable(dsTraCuuDuoc);
            XuatDanhSachHD(dsTraCuuDuoc);
            LamMoi();
            if (gvDS.RowCount == 0)
            {
                MessageBox.Show("Không tìm thấy thông tin do bạn nhập\nXin vui lòng nhập lại thông tin khác", "Thông báo tra cứu");
            }
            //try
            //{
            //    if (txtntt.Text == "" && txtst.Text == "")
            //    {
            //        kqBaoCao = busDT.HoaDonThanhToan();
            //        gvDS.DataSource = kqBaoCao.DefaultView;
            //    }
            //    if (txtntt.Text != "" && txtst.Text == "")
            //    {
            //        kqBaoCao = busDT.TimKiemTheoTenKH(txtntt.Text);
            //        gvDS.DataSource = kqBaoCao.DefaultView;
            //    }

            //    if (txtst.Text != "" && txtntt.Text == "")
            //    {
            //        float t = float.Parse(txtst.Text);
            //        kqBaoCao = busDT.TimKiemTheoSoTienThanhToan(t);
            //        gvDS.DataSource = kqBaoCao.DefaultView;
            //    }
            //}
            //catch { }
            //txtntt.Text = "";
            //txtst.Text = "";
        }

        public void LamMoi()
        {
            txtSoPDK.Text = "";
            txtSoHD.Text = "";
            txtHoTenKH.Text = "";
            txtHoTenNV.Text = "";
            txtCMND.Text = "";
            txtTongTien.Text = "";
            txtMaNV.Text = "";
            dtNgayLapHD.IsEmpty = true;
            txtSoHD.Focus();
        }

        private void txtxuatexel_Click(object sender, EventArgs e)
        {
            string filename = "XuatHoaDonThanhToan.xls";
            XuatHoaDonThanhToan.ExportToExcel(kqBaoCao, filename);
            XuatHoaDonThanhToan.TryToDisplayGeneratedFile(filename);
        }

        private void btxuathtml_Click(object sender, EventArgs e)
        {
            string filename = "XuatHoaDonThanhToan.html";
            XuatHoaDonThanhToan.ExportToHtml(kqBaoCao, filename);
            XuatHoaDonThanhToan.TryToDisplayGeneratedFile(filename);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            LamMoi();
            LayDanhSachHD();
        }
    }
}
